﻿@page "/BookIndex"
@using Carlife.Inf;
@using CarlifeService;
@using Commonality.DTO.AuthorityDTO;
@using ICarlifeService;
@using Microsoft.EntityFrameworkCore;
@using Models.Authority;
@using ICarlifeRespository;
@using CarlifeRespository;
@inject IRolesService ibase;
@inject NavigationManager NavigationManager
@inject IJSRuntime jsRuntime
<PageTitle>角色列表</PageTitle>
<h3>角色列表</h3>
<table class="table-bordered accountTable" width="90%">
    <thead>
        <tr>
            <td>
                <button class="btn btn-primary" @onclick="AddRole">添加</button>
            </td>
        </tr>
        <tr>
            <th>父级角色ID</th>
            <th>账户</th>
            <th>名称</th>
            <th>角色描述</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in books)
        {
            <tr>
                <td>@item.Parent_id</td>
                <td>@item.Account</td>
                <td>@item.RoleName</td>
                <td>@item.Description</td>
                <td>
                    <a href="/UpdateBook/@item.RoleID">编辑</a>
                    <button @onclick="()=>Removeroles(item.RoleID)">删除</button>
                    <a href="">分配权限</a>
                </td>
            </tr>
        }
    </tbody>
</table>
@code {
    private List<RolesDTO> books = new List<RolesDTO>();
    protected override async Task OnInitializedAsync()
    {
        books = ibase.GetRoleAll();
        await base.OnInitializedAsync();
    }
    private void AddRole()
    {
        NavigationManager.NavigateTo($"AddBooks");
    }
    private async void Removeroles(int id)
    {
        bool result = await jsRuntime.InvokeAsync<bool>("confirm", "确定要删除吗?");
        if (result)
        {
            int the = ibase.GetRoleRemove(id);
            if (the > 0)
            {
                await jsRuntime.InvokeAsync<string>("alert", "删除成功 !");
                OnInitializedAsync();
                StateHasChanged();
            }
            else
            {
                await jsRuntime.InvokeAsync<string>("alert", "删除失败 !");
            }
        }
    }
}
